cmake_minimum_required(VERSION 3.19)
project(ev_comm_tool LANGUAGES C CXX)

find_package(Qt6 6.9 REQUIRED COMPONENTS Core Widgets SerialPort)

qt_standard_project_setup()

set(APP_NAME ${PROJECT_NAME})

file(GLOB APP_SRCS source/*.cpp source/*.c source/*.h)
file(GLOB TASK_SRCS task/*.cpp task/*.c task/*.h)
file(GLOB LIB_SRCS library/*.cpp library/*.c library/*.h)
qt_add_executable(${APP_NAME}
    WIN32 MACOSX_BUNDLE
    ${APP_SRCS}
    ${LIB_SRCS}
    ${TASK_SRCS}
)

target_link_libraries(${APP_NAME} PRIVATE
    Qt::Core
    Qt::Widgets
    Qt::SerialPort
)
target_include_directories(${APP_NAME} PRIVATE
    source
    library
    task
)
add_custom_command(TARGET ${APP_NAME} POST_BUILD
    COMMAND ${WINDEPLOYQT_EXECUTABLE}
        --dir "${CMAKE_BINARY_DIR}/deploy"
        --verbose 0
        --no-translations
        "$<TARGET_FILE:${APP_NAME}>"
    COMMAND ${CMAKE_COMMAND} -E copy
            "$<TARGET_FILE:${APP_NAME}>"
            "${CMAKE_BINARY_DIR}/deploy/"
    COMMENT "Deploying Qt for Windows"
)
add_custom_target(deploy_manual
    COMMAND ${WINDEPLOYQT_EXECUTABLE}
        --dir "${CMAKE_BINARY_DIR}/deploy"
        --verbose 0
        --no-translations
        "$<TARGET_FILE:${APP_NAME}>"
    COMMAND ${CMAKE_COMMAND} -E copy
            "$<TARGET_FILE:${APP_NAME}>"
            "${CMAKE_BINARY_DIR}/deploy/"
    DEPENDS ${APP_NAME}
    COMMENT "Manual deployment"
)

include(GNUInstallDirs)

install(TARGETS ${APP_NAME}
    BUNDLE  DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

qt_generate_deploy_app_script(
    TARGET ${APP_NAME}
    OUTPUT_SCRIPT deploy_script
    NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})
